package com.pixiv.servlet.RegisterServlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.pixiv.MysqlDAO.RegisterDAO;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String type = req.getParameter("appType");
        try {
            Class<?> c = this.getClass();
            Method me = c.getMethod("Service_" + type, HttpServletRequest.class, HttpServletResponse.class);
            me.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //初始化注册 register.jsp img 触发 加载背景图片
    public void Service_initRegister(HttpServletRequest req, HttpServletResponse resp)
    {

        resp.setContentType("application/json;charset=utf-8");
        try {
            //初始化图片
            String name = RegisterDAO.getPictureInitName();
            File file = new File("D:\\temp\\pixivPictrue",name);
            FileInputStream fis =  new FileInputStream(file);
            resp.setContentType("application/octet-stream; charset=utf-8");
            resp.setHeader("Content-Disposition", "attachment; filename=" + name);
            ServletOutputStream outputStream = resp.getOutputStream();
            IOUtils.copy(fis, outputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
